package com.dmpp.mes.service.impl;


import com.dmpp.mes.bean.RespEnum;
import com.dmpp.mes.bean.ResponseData;
import com.dmpp.mes.dao.ColorMapper;
import com.dmpp.mes.dao.ColourMapper;
import com.dmpp.mes.parameterInfo.Color;
import com.dmpp.mes.parameterInfo.Colour;
import com.dmpp.mes.service.ColorService;
import com.dmpp.mes.service.ColourService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service(value = "ColourService")

public class ColourServiceImpl implements ColourService {

    @Autowired
    private ColourMapper mapper;

    @Override
    public ResponseData findAll(Colour colour) {
        ResponseData responseData = new ResponseData();
        PageInfo pageInfo = new PageInfo<>(mapper.findAll(colour));
        responseData.setData(pageInfo);
        return responseData;
    }

    @Override
    public ResponseData findByCode(String code) {
        ResponseData responseData = new ResponseData();
        responseData.setData(mapper.findByCode(code));
        return responseData;
    }

    @Override
    public ResponseData insert(Colour Colour) {
        ResponseData responseData = new ResponseData();
        Colour Colour1= mapper.findByCode(Colour.getCode());
        if (Colour1==null){
            mapper.insert(Colour);
        }else {
            responseData.setCodeEnum(RespEnum.ERROR2001);
        }
        return responseData;
    }

    @Override
    public ResponseData updateByCode(Colour Colour) {
        ResponseData responseData = new ResponseData();
        mapper.updateByCode(Colour);
        return responseData;
    }

    @Override
    public ResponseData deleteByCode(String code) {
        ResponseData responseData = new ResponseData();
        mapper.deleteByCode(code);
        return responseData;
    }

    @Override
    public ResponseData delete(String code) {
        ResponseData responseData = new ResponseData();
        int  materialCount= mapper.materialCount(code);
        int  semiProductCount= mapper.semiProductCount(code);
        int  productCount= mapper.productCount(code);
        if (materialCount==0){
            if (semiProductCount==0){
                if (productCount==0){
                    mapper.delete(code);
                }else {
                    responseData.setCodeEnum(RespEnum.ERROR1017);
                }
            }else {
                responseData.setCodeEnum(RespEnum.ERROR1018);
            }
        }else {
            responseData.setCodeEnum(RespEnum.ERROR1016);
        }
        return responseData;
    }

}
